package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    SystemUserService systemUserService;

    @Override
    public CaptchaVo getCaptcha() {
        //验证码图片的长宽高，len是指验证码数据的长度
        SpecCaptcha specCaptcha=new SpecCaptcha(130,48,4);
        //获取验证码的值
        String code = specCaptcha.text().toLowerCase();
        //把验证码和图片结合
        String image = specCaptcha.toBase64();
        //把验证码保存到Redis中
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        redisTemplate.opsForValue().set(key,code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        //将key和base64返回给前端
        return new CaptchaVo(image,key);
    }

    //登录
    @Override
    public String login(LoginVo loginVo) {
        //判断验证码是否有效
        if(!StringUtils.hasText(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //获取redis中的验证码
        Object redisCode = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());

        //redis中的验证码是否过期
        if(redisCode==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        if(!loginVo.getCaptchaCode().equals(redisCode)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }


        //判断账号是否存在
        SystemUser user = systemUserService.getOne
                (new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, loginVo.getUsername()));
        if(user==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //判断账号是否被禁用
        if(user.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        //密码是否正确
        String password = DigestUtils.md5Hex(loginVo.getPassword());
        if(!user.getPassword().equals(password)){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }



        //验证登陆成功
        //创建token  生成令牌
        String token = JwtUtil.createToken(user.getUsername(),user.getId());

        return token;
    }
}
